/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elsimulatorjava;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.Timer;

public class SimulationPanel extends JPanel {
    double[] elevatorPositions;
    Door[] doors;
    Floor[] floors;
    Elevator[] elevators;
    
    final static byte ELEVATOR_HEIGHT = 100;
    final static byte ELEVATOR_WIDTH = 50;
    final static byte BORDER = 12;
    Timer timer;
    int increment = -5;
    int increment1 = -5;
    int widthOfSimulation;
    int heightOfSimulation;
    
    int initialWidth = 0;
    int initialHeight = 0;
    
    public SimulationPanel(Elevator[] e, Floor[] f, Dimension sizeOfPanel) {
        this.floors = f;
        //this.numberOfElevators = elevators.length;
        this.elevators = e;
        
        setLayout(null);
        doors = new Door[elevators.length];
        for (int i = 0; i < elevators.length; i++) {
            doors[i] = new Door(ELEVATOR_WIDTH, ELEVATOR_HEIGHT);
        }
        elevatorPositions = new double[elevators.length];
        for (int i = 0; i < elevators.length; i++) {
            elevatorPositions[i] = elevators[i].getPosition();
        }
        
        widthOfSimulation = 2*BORDER*(1+elevators.length) + doors[0].getElevatorWidth()*elevators.length*2;
        heightOfSimulation = floors.length*ELEVATOR_HEIGHT+BORDER*2;
        
        Dimension finalDimension = new Dimension(widthOfSimulation, heightOfSimulation);
        
        if (heightOfSimulation < sizeOfPanel.height) {
            initialHeight = (sizeOfPanel.height-heightOfSimulation)/2 ;
            finalDimension.setSize(finalDimension.width, sizeOfPanel.height);
        }
        if (widthOfSimulation < sizeOfPanel.width) {
            initialWidth = (sizeOfPanel.width - widthOfSimulation)/2;
            finalDimension.setSize(sizeOfPanel.width-40, finalDimension.height);
        }
        
        this.setPreferredSize(finalDimension);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //elevatorPositions = elevators;
        g.setColor(Color.black);
        //g.fillRect(0, 0, getWidth(), getHeight());
        g.drawLine(BORDER+initialWidth, BORDER+initialHeight, initialWidth+widthOfSimulation-BORDER, initialHeight+BORDER);
        for (int i = 0; i < floors.length; i++) {
            int height = initialHeight+heightOfSimulation-(BORDER+ELEVATOR_HEIGHT*i);
            g.drawLine(BORDER+initialWidth, initialHeight+height, initialWidth+widthOfSimulation-BORDER, initialHeight+height);
            int finished = 0;
            int waiting = 0;
            for (Person person : floors[i].getPeople()) {
                if (person.isFinished()){
                    finished++;
                } else {
                    waiting++;
                }
            }
            g.setColor(Color.black);
            g.drawString(String.valueOf(waiting), BORDER+initialWidth, initialHeight+heightOfSimulation-BORDER-ELEVATOR_HEIGHT*i-ELEVATOR_HEIGHT/2-3);
            g.drawString(String.valueOf(finished), initialWidth+widthOfSimulation-20, initialHeight+heightOfSimulation-BORDER-ELEVATOR_HEIGHT*(i)-ELEVATOR_HEIGHT/2-3);
        }
        
        int i = 0;
        for (Elevator elevator : elevators) {
            //System.out.println(position);
            doors[i].setOpening(elevators[i].getDoor());
            doors[i].draw(g, initialWidth+BORDER*2+doors[i].getElevatorWidth()*2*i + 2*BORDER*i, (int)(initialHeight+heightOfSimulation-BORDER-ELEVATOR_HEIGHT*(1+elevator.getProperPosition())));
            i++;
        }
    }
}
